package com.njxzc.demo.service.before;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njxzc.demo.common.http.ResponseResult;
import com.njxzc.demo.entity.CartEntity;
import com.njxzc.demo.mapper.before.CartMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Transactional
@Service
public class CartServiceImpl extends ServiceImpl<CartMapper, CartEntity> implements CartService{
    @Override
    public ResponseResult<Map<String, Object>> addCart(CartEntity cartEntity) {
        Long res = lambdaQuery().eq(CartEntity::getBusertableId, cartEntity.getBusertableId())
                .eq(CartEntity::getGoodstableId, cartEntity.getGoodstableId()).count();
        boolean flag = false;
        if(res > 0){
            UpdateWrapper<CartEntity> updateWrapper =  new UpdateWrapper<>();
            updateWrapper.setSql("shoppingnumber= shoppingnum +" + cartEntity.getShoppingnum());
            updateWrapper.last("where busertable is = "+ cartEntity.getBusertableId() +
                    "and goodstable_id = "+ cartEntity.getBusertableId());
            flag = update(updateWrapper);

        }else{ //购物车没有该商品
            flag = save(cartEntity);
        }
        if(flag){
            //成功 标志A001
            return ResponseResult.getMessageResult(null,"A001");
        }else{
            //失败 标志A002
            return ResponseResult.getMessageResult(null,"A002");
        }

    }

    @Override
    public ResponseResult<Map<String, Object>> bupDateCart(CartEntity cartEntity) {
        List<Integer> bcid = cartEntity.getBcid();
        List<Integer> bshoppingnum = cartEntity.getBshoppingnum();

        ArrayList<CartEntity> bCarts = new ArrayList<CartEntity>();
        for(int i=0; i<bcid.size(); i++){
            CartEntity cart = new CartEntity();
            cart.setId(bcid.get(i));
            cart.setShoppingnum(bshoppingnum.get(i));
            bCarts.add(cart);
//            System.out.println(cart);
        }
//        System.out.println(bCarts);
//        return ResponseResult.getMessageResult(null,"A001");
        updateBatchById(bCarts);
        return ResponseResult.getMessageResult(null,"A001");
    }

    @Override
    public ResponseResult<Map<String, Object>> clearCart(CartEntity cartEntity) {
//        LambdaQueryWrapper<CartEntity> lqw = new LambdaQueryWrapper<CartEntity>();
//        lqw.eq(CartEntity::getBusertableId, cartEntity.getBusertableId());
//        remove(lqw);

        removeById(cartEntity.getBusertableId());
        return ResponseResult.getMessageResult(null, "A001");
    }

    @Override
    public ResponseResult<Map<String, Object>> removeCart(CartEntity cartEntity) {
        removeById(cartEntity);
        return ResponseResult.getMessageResult(null, "A001");
    }
}
